package filters

import javax.inject.Inject

import akka.stream.Materializer
import play.api.Logger
import play.api.mvc.{Filter, RequestHeader, Result}
import play.api.routing.Router.Tags
import repos.UserRepository

import scala.concurrent.{ExecutionContext, Future}

class AuthFilter @Inject()(val userRepository: UserRepository)(implicit val mat: Materializer, ec: ExecutionContext) extends Filter {

  override def apply(nextFilter: (RequestHeader) => Future[Result])(requestHeader: RequestHeader): Future[Result] = {

    val startTime = System.currentTimeMillis()

    nextFilter(requestHeader).map {
      result =>
        val action = requestHeader.tags(Tags.RouteController) + "." +
          requestHeader.tags(Tags.RouteActionMethod)
        val endTime = System.currentTimeMillis()
        val requestTime = endTime - startTime
        Logger.info(s"$action took ${requestTime}ms and returned ${result.header.status}")
        result.withHeaders("Request-Time"->requestTime.toString)
    }
  }
}
